package org.example.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.example.pojo.User;
import org.example.pojo.UserPwd;
import org.example.pojo.Variable;
import org.example.service.UserService;
import org.example.service.VarService;
import org.example.util.JWTUtils;
import org.example.util.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/var")
@Api("变量信息")
public class VariableController {


    @Autowired
    VarService varService;


    public HttpServletRequest request;


    @PostMapping("/showVars")
    @ResponseBody
    @ApiOperation("展示变量")
    public ResponseData showVars(int page,int limit){

        //使用PageHelper开启分页功能
        PageHelper.startPage(page,limit);

        //紧跟着PageHelper.startPage(page,limit)的下一条查询语句会被拦截
        //添加分页实现,并且查询了此sql对应的count
        List<Variable> variables =  varService.selectVariables();

        //根据查询出来的集合生成一个PageInfo对象
        PageInfo<Variable> userPageInfo = new PageInfo<>(variables);


        ResponseData jsonData = ResponseData.getJsonData(variables,(int)userPageInfo.getTotal());

        return jsonData;


    }





    @PostMapping("/editVar")
    @ResponseBody
    @ApiOperation("编辑变量")
    public ResponseData edit(@RequestBody Variable variable) {


        varService.deleteVar(variable.getvId());

        String value = variable.getvValue();
        System.out.println(value);
        List<String> values = Arrays.asList(value.split("\\|"));
        for (String v:values) {
            System.out.println(v);
            variable.setvValue(v);
            varService.insertVar(variable);
        }


        return ResponseData.getSccesss("修改成功",100);

    }

    @GetMapping("/selectByAll/{page}/{limit}/{var}")
    @ResponseBody
    @ApiOperation("模糊查询变量")
    public ResponseData selectByAll(@ApiParam("当前页") @PathVariable int page, @ApiParam("每一页显示几条数据") @PathVariable int limit, @ApiParam("模糊查询输入的内容") @PathVariable String var) {


        PageHelper.startPage(page,limit);


        Map<String,String> map = new HashMap<>();

        List<Variable> variables = varService.selectVariablesByAll(var);
        //根据查询出来的集合生成一个PageInfo对象
        PageInfo<Variable> variablePageInfo = new PageInfo<>(variables);
        ResponseData responseData = ResponseData.getJsonData(variables,(int)variablePageInfo.getTotal());
        return responseData;

    }

}
